package com.ruoyi.web.controller.template;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.template.domain.Template;
import com.ruoyi.template.domain.TemplateUser;
import com.ruoyi.template.dto.TemplateDTO;
import com.ruoyi.template.dto.TemplateRequest;
import com.ruoyi.template.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/template/TemplateController")
@Valid
public class TemplateController extends BaseController{

    @Autowired
    private TemplateService templateService;

    /**
     *审核管理-新增模板
     * @param templateRequest
     * @return
     */
    @PreAuthorize("@ss.hasPermi('template:TemplateController:add')")
    @Log(title = "新增模板", businessType = BusinessType.INSERT)
    @PostMapping("/saveTemplate")
    public AjaxResult insertTemplate(@Valid @RequestBody TemplateRequest templateRequest) {

        return toAjax(templateService.saveTemplate(templateRequest,getNickname()));
    }
    /**
     * 查询模板列表
     */
    @PreAuthorize("@ss.hasPermi('template:TemplateController:list')")
    @GetMapping("/list")
    public TableDataInfo list(Template template)
    {
        startPage();
        List<TemplateDTO> list = templateService.selectTemplateList(template);
        return getDataTable(list);
    }
    /**
     * 获取模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('template:TemplateController:query')")
    @GetMapping(value = "/getTemplate/{templateId}")
    public TemplateDTO getTemplate(@PathVariable("templateId") String templateId)
    {
        return templateService.getTemplateWithDetails(templateId);
    }
    /**
     *审核管理-修改模板
     * @param templateRequest
     * @return
     */
    @PreAuthorize("@ss.hasPermi('template:TemplateController:edit')")
    @Log(title = "修改模板", businessType = BusinessType.INSERT)
    @PostMapping("/updateTemplate")
    public AjaxResult updateTemplate(@Valid @RequestBody TemplateRequest templateRequest) {

        return toAjax(templateService.updateTemplate(templateRequest,getNickname()));
    }
    /**
     * 获取菜单列表（排除按钮）
     */
    @GetMapping("/getMenuList")
    public AjaxResult getMenuList(SysMenu menu)
    {
        List<SysMenu> menus = templateService.selectMenuList(menu);
        return success(menus);
    }
    /**
     *新增/修改指定用户
     * @param templateUser
     * @return
     */
    @PreAuthorize("@ss.hasPermi('template:TemplateController:add')")
    @Log(title = "指定用户", businessType = BusinessType.INSERT)
    @PostMapping("/saveTemplateUser")
    public AjaxResult saveTemplateUser(@Valid @RequestBody TemplateUser templateUser) {

        return toAjax(templateService.saveTemplateUser(templateUser));
    }
}
